package bill.study.algorithms.arraysStrings;

import java.util.HashMap;

public class ArraysAndStrings {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String s = "helloworld";
		System.out.println(removeDuplicate(s));

	}

	/**
	 * Cracking the interview, fourth edition 1.3 
	 * remove duplicate characters
	 * from string
	 * 
	 * Solution 1
	 */
	public static String removeDuplicate(String original) {
		if(original == null){
			throw new NullPointerException("");
		}
		if(original.isEmpty()){
			return original;
		}
		StringBuilder temp = new StringBuilder();
		HashMap<Character, Character> hash = new HashMap<Character, Character>();
		for (int i = 0; i < original.length(); i++) {

			Character ch = hash.get(original.charAt(i));
			if (ch == null) {
				hash.put(original.charAt(i), original.charAt(i));
				temp.append(original.charAt(i));
			} else {
				// duplicate, skip it
			}
		}
		return temp.toString();
	}

}
